<template>
  <mk-echart :options="options" :key="key"></mk-echart>
</template>

<script>
import MkEchart from "../../libs/index.vue";
export default {
  name: "chartFour",
  components: {
    MkEchart,
  },
  props: {
    xData: {
      type: Array,
      default: () => [],
    },
    yData: {
      type: Array,
      default: () => [],
    },
    name: {
      type: Array,
      default: () => [],
    },
    value: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    xData: {
      deep: true,
      handler(val) {
        this.$nextTick(() => {
          this.key = new Date().getTime();
        });
      },
    },
    value: {
      deep: true,
      handler(newVal, oldVal) {
        this.value = newVal;
        this.setOptions();
      },
    },
  },
  created() {
    this.setOptions();
  },
  mounted() {
    this.setOptions();
  },
  data() {
    return {
      key: new Date().getTime(),
      val: 0,
      options: {},
    };
  },
  methods: {
    setOptions() {
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        legend: {
          data: this.name,
          bottom: "10%",
          itemWidth: 10,
          itemHeight: 10,
          textStyle: { color: "#999999" },
          padding: [23, -15, 0, 0],
        },
        grid: {
          top: "3%",
          left: "2%",
          right: "2%",
          bottom: "20%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.xData,
            axisTick: { show: false },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#dddddd",
              },
            },
            axisLabel: { color: "#333333", fontSize: 10 },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLabel: {
              formatter: "{value} %",
            },
            splitLine: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#dddddd",
              },
            },
            axisLabel: { color: "#999999", fontSize: 10 },
          },
        ],
        series: [
          {
            name: this.name[0],
            // barWidth: 50, // 柱子宽度，不设置时宽度自适应
            type: "bar",
            position: "top",
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "#333333",
                fontSize: 10,
              },
              formatter: "{c}%",
            },
            itemStyle: {
              color: "#EDEDF0",
            },
            data: this.value[0],
            z: 1,
          },
          {
            name: this.name[1],
            // barWidth: 50, // 柱子宽度，不设置时宽度自适应
            type: "bar",
            position: "top",
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "#333333",
                fontSize: 10,
              },
              formatter: "{c}%",
            },
            itemStyle: {
              color: "#D71B21",
            },
            data: this.value[1],
            z: 999999,
          },
          {
            xAxisIndex: 1,
            name: this.name[0],
            type: "line",
            showSymbol: false, // 不显示
            itemStyle: {
              normal: {
                lineStyle: {
                  color: "#EDEDF0",
                  type: "solid", //'dotted'虚线 'solid'实线
                },
              },
            },
            data: this.value[0],
          },
          {
            xAxisIndex: 1,
            name: this.name[1],
            type: "line",
            showSymbol: false, // 不显示
            itemStyle: {
              normal: {
                lineStyle: {
                  color: "#D71B21",
                  type: "solid",
                },
              },
            },
            data: this.value[1],
          },
        ],
      };
      // 增加了一个隐藏的x轴，用来控制线图的点的位置
      option.xAxis[1] = {
        type: "value",
        max: option.xAxis[0].data.length * 100,
        show: false,
      };
      if (option.series[2].data) {
        option.series[2].data = option.series[2].data.map((x, i) => [30 + i * 100, x]);
      }
      if (option.series[3].data) {
        option.series[3].data = option.series[3].data.map((x, i) => [70 + i * 100, x]);
      }
      this.options = option;
    },
  },
};
</script>

<style>
</style>
